package utilities;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;

public class Utils {
	private final static int infinity=2147483647;
	public final static String arff = "arff";
    public final static String jar = "jar";
    public final static String zip = "zip";
    public final static String col = "col";

    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');
 
        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
    
    public static double findMax(double[] array){
    	double max = 0;
    	for (int i=0;i<array.length;i++){
    		if (array[i]>max)
    			max = array[i];
    	}
    	return max;
    }
    
    public static int findMax(int[] array){
    	int max = 0;
    	for (int i=0;i<array.length;i++){
    		if (array[i]>max)
    			max = array[i];
    	}
    	return max;
    }
    
    public static double findMin(double[] array){
    	double min = infinity;
    	for (int i=0;i<array.length;i++){
    		if (array[i]<min)
    			min = array[i];
    	}
    	return min;
    }
    
    public static int findMin(int[] array){
    	int min = infinity;
    	for (int i=0;i<array.length;i++){
    		if (array[i]<min)
    			min = array[i];
    	}
    	return min;
    }
    
    public static double calculateMean(double[] array){
    	double mean = 0;
    	double sum = 0;
    	for (int i=0;i<array.length;i++)
    		sum+=array[i];
    	mean = sum/ ((double) array.length);
    	return mean;
    }
    
    public static double calculateMean(int[] array){
    	double mean = 0;
    	double sum = 0;
    	for (int i=0;i<array.length;i++)
    		sum+=array[i];
    	mean = ((double) sum)/ ((double) array.length);
    	return mean;
    }
    
    public static double calculateStandardDeviation(double[] array){
    	double mean = 0;
    	double sum = 0;
    	double std = 0;
    	double numStd = 0;
    	for (int i=0;i<array.length;i++)
    		sum+=array[i];
    	mean = ((double) sum)/ ((double) array.length);
    	
    	for (int i =0; i<array.length;i++)
    		numStd += (array[i]-mean)*(array[i]-mean);
    	std = Math.sqrt(numStd/array.length);
    	return std;
    }
    
    public static double calculateStandardDeviation(int[] array){
    	double mean = 0;
    	double sum = 0;
    	double std = 0;
    	double numStd = 0;
    	for (int i=0;i<array.length;i++)
    		sum+=array[i];
    	mean = ((double) sum)/ ((double) array.length);
    	
    	for (int i =0; i<array.length;i++)
    		numStd += (array[i]-mean)*(array[i]-mean);
    	std = Math.sqrt(numStd/array.length);
    	return std;
    }
    
    public static double findMedian(int[] array){
    	double median = 0;
    	ArrayList<Integer> sortedList = new ArrayList<Integer>();
    	for (int i=0;i<array.length;i++){
    		sortedList.add(array[i]);
    	}
    	Collections.sort(sortedList);
    	if (array.length % 2==0) //even length
    		median = (sortedList.get(array.length/2)+
    				  sortedList.get((array.length/2)+1)/2);
    	else //odd length
    		median = sortedList.get((array.length+1)/2);
    	return median;
    }
    
    public static double findMedian(double[] array){
    	double median = 0;
    	ArrayList<Double> sortedList = new ArrayList<Double>();
    	for (int i=0;i<array.length;i++){
    		sortedList.add(array[i]);
    	}
    	Collections.sort(sortedList);
    	if (array.length % 2==0) //even length
    		median = (sortedList.get(array.length/2)+
    				  sortedList.get((array.length/2)+1)/2);
    	else //odd length
    		median = sortedList.get((array.length+1)/2);
    	return median;
    }
    
    public static double findFirstQuartile(double[] array){
    	double q1 = 0;
    	//Sort the list
    	ArrayList<Double> sortedList = new ArrayList<Double>();
    	for (int i=0;i<array.length;i++){
    		sortedList.add(array[i]);
    	}
    	Collections.sort(sortedList);

    	//Find the median Index
    	int medianIndex = 0;
    	if (array.length % 2==0) //even length
    		medianIndex = array.length/2;
    	else //odd length
    		medianIndex = (array.length+1)/2;
    	
    	//Find the first quartile
    	if (medianIndex % 2==0) //even length
    		q1 = (sortedList.get(medianIndex/2)+sortedList.get((medianIndex/2)+1))/2;
    	else //odd length
    		q1 = sortedList.get(((medianIndex/2)+1)/2);
    	return q1;
    }

    public static double findFirstQuartile(int[] array){
    	double q1 = 0;
    	
    	//Sort the list
    	ArrayList<Integer> sortedList = new ArrayList<Integer>();
    	for (int i=0;i<array.length;i++){
    		sortedList.add(array[i]);
    	}
    	Collections.sort(sortedList);

    	//Find the median Index
    	int medianIndex = 0;
    	if (array.length % 2==0) //even length
    		medianIndex = array.length/2;
    	else //odd length
    		medianIndex = (array.length+1)/2;
    	
    	//Find the first quartile
    	if (medianIndex % 2==0) //even length
    		q1 = (sortedList.get(medianIndex/2)+sortedList.get((medianIndex/2)+1))/2;
    	else //odd length
    		q1 = sortedList.get(((medianIndex/2)+1)/2);
    	return q1;
    }
    
    public static double findThirdQuartile(int[] array){
    	double q3 = 0;
    	
    	//Sort the list
    	ArrayList<Integer> sortedList = new ArrayList<Integer>();
    	for (int i=0;i<array.length;i++){
    		sortedList.add(array[i]);
    	}
    	Collections.sort(sortedList);

    	//Find the median Index
    	int medianIndex = 0;
    	if (array.length % 2==0) //even length
    		medianIndex = array.length/2;
    	else //odd length
    		medianIndex = (array.length+1)/2;
    	
    	//Find the third quartile
    	int upperHalfSize = sortedList.size()-medianIndex;
    	if (upperHalfSize % 2==0) //even length
    		q3 = (sortedList.get((upperHalfSize/2)+medianIndex-1)+
    			  sortedList.get((upperHalfSize/2)+medianIndex))/2;
    	else //odd length
    		q3 = sortedList.get((((upperHalfSize/2)+1)/2)+medianIndex-1);
    	return q3;
    }
    
    public static double findThirdQuartile(double[] array){
    	double q3 = 0;
    	//Sort the list
    	ArrayList<Double> sortedList = new ArrayList<Double>();
    	for (int i=0;i<array.length;i++){
    		sortedList.add(array[i]);
    	}
    	Collections.sort(sortedList);

    	//Find the median Index
    	int medianIndex = 0;
    	if (array.length % 2==0) //even length
    		medianIndex = array.length/2;
    	else //odd length
    		medianIndex = (array.length+1)/2;
    	
    	//Find the third quartile
    	int upperHalfSize = sortedList.size()-medianIndex;
    	if (upperHalfSize % 2==0) //even length
    		q3 = (sortedList.get((upperHalfSize/2)+medianIndex-1)+
    			  sortedList.get((upperHalfSize/2)+medianIndex))/2;
    	else //odd length
    		q3 = sortedList.get((((upperHalfSize/2)+1)/2)+medianIndex-1);
    	return q3;
    }
}
